﻿using System;
using System.Web;
using Utilities;
using SocialShare.BusinessLogicLayer;
using SocialShare.BusinessEntities;
using SocialShare.Web.Integrations;
using SocialShare.BLL;
using System.Configuration;
using SocialShare.DAL.Facades;

public partial class forum_submit_post : System.Web.UI.Page
{
    private Int32 UserID
    {
        get
        {
            try
            {
                
                if (Request.QueryString["id"]!=null && !string.IsNullOrEmpty(Request.QueryString["id"].ToString()))
                {
                    return Convert.ToInt32(Request.QueryString["id"].ToString());
                }
                else
                {
                    return Convert.ToInt32(HttpContext.Current.Session["Id"].ToString());
                }
            }
            catch
            {
                return -1;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropdown();
        }
    }

    private void LoadDropdown()
    {
        ForumFacade bll = new ForumFacade();
        ddlCategory.DataSource = bll.GetAllForumCategories();
        ddlCategory.DataBind();
    }
    protected void lnbAddNew_Click(object sender, EventArgs e)
    {
        ConfigReader configReader = new ConfigReader();
        if (string.IsNullOrEmpty(hidCKEDitorValue.Value))
        {
            divMessage.Visible = true;
            divMessage.Attributes.Add("class", "error");
            lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.EMPTY_MESSAGE.ToString());
        }
        else
        {
            ForumFacade bll = new ForumFacade();
            StrategyProxyUser currentLoggedInUser=new StrategyProxyUser();
            SocailShare_Forum_Messages message = new SocailShare_Forum_Messages();
            message.CategoryId =Convert.ToInt32(ddlCategory.SelectedItem.Value);
            message.CreateDate = DateTime.Now;
            message.MessageBody = Server.HtmlEncode(hidCKEDitorValue.Value.Trim());
            message.Status = (Int32)ForumStatus.Published;
            message.UserId = currentLoggedInUser.GetCurrentUser().User_ID;
            message.MessageSubject = txtSubject.Value.Trim();

            Int32 postId = bll.CreateNewForum_Messages(message);
            if (postId > 0)
            {
                Response.Redirect("~/discussion-board/");
            }
            else
            {
                divMessage.Visible = true;
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.FAILED_MESSAGE_POST.ToString());
            }
        }
    }
}